package com.snimay.app.item;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.snimay.app.entity.repository.EntityRepository;
import com.snimay.app.entity.vo.Entity;
import com.snimay.app.entity.vo.EntityInfo;
import com.snimay.util.ToWeb;

/**   
 * @Title      : EntityController.java
 * @Package    : com.snimay.app.entity
 * @Description: 实体的 
 * @author     : xxy
 * @date       : 2018年5月7日 下午4:22:43
 * @version    : V1.0   
 */
@RestController
@RequestMapping("Item")
public class ItemController  {
	@Autowired
	EntityRepository repository;
	//==========================================================
	
	@GetMapping("/getItem/{clazz}/{key}/{value}")
	public Object getItem(
			@PathVariable("clazz") String clazz,
			@PathVariable("key") String key,
			@PathVariable("value") String value) {
		
		ToWeb toweb = new ToWeb();
		List<Entity> lis	=	repository.findByClazz(clazz);
		
		if(lis==null&&lis.size()==0) {
			return new Gson().toJson(toweb);
		}
		
		if(key==null&&key.length()==0) {
			toweb.setObjData(lis.get(0));
			return new Gson().toJson(toweb);
		}
		if(key=="id") {
			Entity en = repository.findOne(Long.parseLong(value));
			toweb.setObjData(en);
			return new Gson().toJson(toweb); 
		}else {
			for(Entity en : lis) {
				for(EntityInfo ef: en.info) {
					if(ef.key.equals(key)&&Pattern.matches(value, ef.value)){
						toweb.setObjData(en);
						return new Gson().toJson(toweb); 
					}
				}
			} 
		}
	
		return new Gson().toJson(toweb);
	}
	
	@PutMapping("/setItem/{clazz}/{key}/{value}")
	public Object setItem(
			@PathVariable("clazz") String clazz,
			@PathVariable("key") String key,
			@PathVariable("value") String value) {
		ToWeb toweb = new ToWeb();
		List<Entity> lis	=	repository.findByClazz(clazz);
		
		if(lis==null&&lis.size()==0) {
			return new Gson().toJson(toweb);
		}
		
		if(key==null&&key.length()==0) {
			toweb.setObjData(lis.get(0));
			return new Gson().toJson(toweb);
		}
		if(key=="id") {
			Entity en = repository.findOne(Long.parseLong(value));
			for(EntityInfo ef: en.info) {
				if(ef.key.equals(key)){
					ef.value = value;
					repository.save(en);
					return new Gson().toJson(toweb);
				}
			}
		}else {
			for(Entity en : lis) {
				for(EntityInfo ef: en.info) {
					if(ef.key.equals(key)){
						ef.value = value;
						repository.save(en);
						return new Gson().toJson(toweb);
					}
				}
			} 
		}
		
		return new Gson().toJson(toweb);
	}
	
	
	
	@PutMapping("/PutItem/{clazz}/{key}/{value}")
	public Object PutItem(
			@PathVariable("clazz") String clazz,
			@PathVariable("key") String key,
			@PathVariable("value") String value,
			@RequestBody Entity obj) {
		ToWeb toweb = new ToWeb();
		List<Entity> lis	=	repository.findByClazz(clazz);
		
		if(lis==null&&lis.size()==0) {
			return new Gson().toJson(toweb);
		}
		
		if(key==null&&key.length()==0) {
			toweb.setObjData(lis.get(0));
			return new Gson().toJson(toweb);
		}
		
		if(key=="id") {
			Entity en = repository.findOne(Long.parseLong(value));
			en.info = obj.info;
			repository.save(en);
		}else {
			for(Entity en : lis) {
				for(EntityInfo ef: en.info) {
					if(ef.key.equals(key)&&Pattern.matches(value, ef.value)){
						en.info = obj.info;
						repository.save(en);
						return new Gson().toJson(toweb);
					}
				}
			} 
		}
		return new Gson().toJson(toweb);
	}
	
	
	@PostMapping("/PostItem/{clazz}/")
	public Object PostItem(
			@PathVariable("clazz") String clazz,
			@RequestBody Entity entity) {
		entity.clazz = clazz;
		entity = repository.save(entity);
		ToWeb toweb = new ToWeb();
		toweb.setObjData(entity);
		return new Gson().toJson(toweb);
	}
	
	
	
	
	
	
	@GetMapping("/GetItems/{clazz}/{query}/{size}/{start}")
	public Object getItems(
			@PathVariable("clazz") String clazz,
			@PathVariable("query") String query,
			@PathVariable("size") String size,
			@PathVariable("start") String start) {
		ToWeb toweb = new ToWeb();
		List<Entity> lis	=	repository.findByClazz(clazz);
		List<Entity> ls		=	new ArrayList<Entity>();
		if(size==null||size.length()==0) {
			size	=	"100";
		}
		if(start==null||start.length()==0) {
			size	=	"0";
		}
		if(query==null||query.length()==0) {
			query	=	"id:*";
		}
		String key 	= query.split(":")[0];
		String value= query.split(":")[1];
		if(key=="id") {
			for(Entity en : lis) {
				 if((en.id+"").equals(value)) {
					 ls.add(en);
				 }
			} 
		}else {
			for(Entity en : lis) {
				for(EntityInfo ef: en.info) {
					if(ef.key.equals(key)&&Pattern.matches(value, ef.value)){
						ls.add(en);break;
					}
				}
			} 
		}
		ls.subList(Integer.parseInt(start),Integer.parseInt(start)+Integer.parseInt(size));
		toweb.setObjData(ls);
		return new Gson().toJson(toweb);
	}
	@GetMapping("/GetItemsCount/{clazz}/{query}")
	public Object getItemsCount(
			@PathVariable("clazz") String clazz,
			@PathVariable("query") String query ) {
		ToWeb toweb = new ToWeb();
		List<Entity> lis	=	repository.findByClazz(clazz);
		List<Entity> ls		=	new ArrayList<Entity>();
		 
		String key 	= query.split(":")[0];
		String value= query.split(":")[1];
		if(key=="id") {
			for(Entity en : lis) {
				 if((en.id+"").equals(value)) {
					 ls.add(en);
				 }
			} 
		}else {
			for(Entity en : lis) {
				for(EntityInfo ef: en.info) {
					if(ef.key.equals(key)&&Pattern.matches(value, ef.value)){
						ls.add(en);break;
					}
				}
			} 
		}
		toweb.setObjData(ls.size());
		return new Gson().toJson(toweb);
	}
	
	
	
	
	
	
}
